
package mail.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import mail.util.Mail;

/**
 * Wrapper for a button that is connected
 * to a single mail.
 *
 * @author jonas.lindmark
 */
public class MailButton implements ActionListener{
    private Mail mail;
    public MailButton(Mail mail)
    {
        this.mail = mail;
    }
    /**
     * Get a button linked to this mail
     *
     * @return The button
     */
    public JButton getButtonRepresentation()
    {
        JButton button = new JButton();
        String text = mail.getSubj() + "\n[" + mail.getFrom()+"]";
        button.setText(text);
        button.addActionListener(this);
        return button;
    }
    /**
     * Handles if one of the buttons created by
     * this.getButtonRepresentation() has been clicked.
     * @param e The event
     */
    public void actionPerformed(ActionEvent e) {
        MailFrame mf = new MailFrame(mail);
        mf.setVisible(true);
        MainFrame main = MainFrame.getInstance();
        if (main != null)
        {
            main.addToDesktop(mf);
        }
    }

}
